#include <vector>
#include <cstdlib>
#include <ctime>
#include <iostream>

#include "../../source/linked_list/node.h"
#include "../../source/linked_list/list_api.h"

void args_processing(int argc, char ** argv, std::vector<int> & nums)
{
    int cnt = 10;
    if(argc == 2)
    {
        cnt = std::atoi(argv[1]);
    }
    if(argc < 3)
    {
        srand(time(NULL));
        for(int i = 0; i < cnt; ++ i)
        {
            nums.push_back(rand() % 100);
        }
    }
    else
    {
        for(int i = 1; i < argc; ++ i)
        {
            nums.push_back(std::atoi(argv[i]));
        }
    }
}

int main(int argc, char ** argv)
{
    // Get the sequence either from input or generate
    std::vector<int> nums;
    args_processing(argc, argv, nums);

    std::cout << "Sequence of length " << nums.size() << ":" << std::endl;
    std::vector<int>::iterator it;
    for(it = nums.begin(); it != nums.end(); ++ it)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl << std::endl;

    // create linked list from the sequence
    Node * head = deserialize(&nums.at(0), nums.size());
    std::cout << "Linked list created from input:" << std::endl;
    display_till_end(head);
    std::cout << std::endl;

    // in place reversal
    reverse_in_place1(head);
    std::cout << "Reverse once:" << std::endl;
    display_till_end(head);
    std::cout << std::endl;

    // in place reversal 2
    head = reverse_in_place2(head, NULL);
    std::cout << "Reverse again:" << std::endl;
    display_till_end(head);
    std::cout << std::endl;

    // serialize to array
    const int size = 100;
    int seq[size];
    serialize(head, seq, size);

    int len = head->count_to_end();
    std::cout << "Serialize to an array:" << std::endl;
    for(int i = 0; i < len; ++ i)
    {
        std::cout << seq[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}
